<?php
class Google_Events
{
	
	public function getRawEventData($subCalendarId, $startTimestamp = NULL, $numDays = NULL)
	{
		
		if(empty($subCalendarId)) { $subCalendarId = 'default'; }
		if(empty($numDays)) { $numDays = 30; }
		if(empty($startTimestamp)) { 
			$startDate = date("Y-m-d h:i:s"); //today
			$startTime = time(); 
		} else {
			$startDate = date("Y-m-d h:i:s", $startTimestamp); //today
			$startTime = $startTimestamp; 		
		}
		   		
		$this->gdata = new Zend_Gdata_Calendar;
		$calendar_url = 'http://www.google.com/calendar/feeds/'.$subCalendarId.'/public/full';
		
		
		$endTime = ($numDays * 86400) + $startTime;
		$endDate = date("Y-m-d", $endTime) . ' 23:59:59';

		$query = $this->gdata->newEventQuery();
		$query->setUser($subCalendarId);
		$query->setOrderby('starttime');
		$query->setSortOrder('ascending');
		$query->setSingleEvents('true'); // makes all recurring events into single events for each day it occurs
		$query->setProjection('full');
		$query->setFutureevents('true');
		$query->setStartMin($startDate);
		$query->setStartMax($endDate);

		try {
			$events = $this->gdata->getCalendarEventFeed($query);
		} catch (Zend_Gdata_App_Exception $e) {
			throw new Exception("Error: " . $e->getMessage());
		} 
		$i = 1;	    
	    $eventData = array();
		foreach ($events as $event) {
			
	    	foreach ($event->where as $where) {	    		
	    		// suck out the dates/times for the event
	            $location = $where->valueString;
	            $whereMap[$i] = array('location'=>$location);		
	        } 
	    	
	    	foreach ($event->when as $when) {
	            // suck out the dates/times for the event
	    		
	    		$start = $when->startTime;
	            $end = $when->endTime;     
	            $whenMap[$i] = array('title'=>$event->title->text, 'date'=>date('Y-m-d', strtotime($start)), 'day'=>date('l', strtotime($start)), 'start'=>strtotime($start), 'end'=>strtotime($end));		
	        	$eventData[$i] = array_merge($whenMap[$i], $whereMap[$i]);
	        	
	    	} 
	    	$i++;
	        
	    }
	    
	    function cmp($a, $b) { 
	    	if($a['date'] == $b['date']) { return 0; }
	    	return ($a['date'] < $b['date']) ? -1 : 1; } 
	    
	    if(!empty($eventData)){
	    	usort($eventData, 'cmp');
	    }
	    
	    return $eventData;
	}
	
	public function getUpcomingWeeklyEventSchedule($subCalendarId)
	{
		
		$numDays = 7;
		   		
		$this->gdata = new Zend_Gdata_Calendar;
		$calendar_url = 'http://www.google.com/calendar/feeds/'.$subCalendarId.'/public/full';
		
		$startDate = date("Y-m-d h:i:s"); //today
		$startTime = time();
		$endTime = ($numDays * 86400) + $startTime;
		$endDate = date("Y-m-d", $endTime) . ' 23:59:59';

		$query = $this->gdata->newEventQuery();
		$query->setUser($subCalendarId);
		$query->setOrderby('starttime');
		$query->setSortOrder('ascending');
		$query->setSingleEvents('true'); // makes all recurring events into single events for each day it occurs
		$query->setProjection('full');
		$query->setFutureevents('true');
		$query->setStartMin($startDate);
		$query->setStartMax($endDate);

		try {
			$events = $this->gdata->getCalendarEventFeed($query);
		} catch (Zend_Gdata_App_Exception $e) {
			throw new Exception("Error: " . $e->getMessage());
		} 
			    
	    foreach ($events as $event) {
			
	    	foreach ($event->when as $when) {
	            // suck out the dates/times for the event
	            $start = $when->startTime;
	            $end = $when->endTime;     
	            $eventMap[] = array('title'=>$event->title->text, 'date'=>date('Y-m-d', strtotime($start)), 'day'=>date('l', strtotime($start)), 'start'=>strtotime($start), 'end'=>strtotime($end));		
	        } 
	    }
	    
	    $eventData = array();
	    
	    function compare($a, $b) { 
	    	if($a['start'] == $b['start']) { return 0; }
	    	return ($a['start'] < $b['start']) ? -1 : 1; } 
	    
	    if(!empty($eventMap)) {
	    	usort($eventMap, 'compare');
	    
	    
	    
		    $dayList = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
		    
		    /*foreach ($eventMap as $e) {
		     	$eventData[$e['title']] = array();
		    }*/
		     	
			foreach ($eventMap as $e) {
				$eventData[$e['title']]['title'] = $e['title'];
				foreach ($dayList as $d) {
					$eventData[$e['title']][$d] = 'closed';
				}
		    }
	
		    $dayArray = array();
		    
			foreach ($eventMap as $e) {
				if($eventData[$e['title']][$e['day']] == 'closed') {
	
					$eventData[$e['title']][$e['day']] = date("g:ia", $e['start']) . ' - ' . date("g:ia", $e['end']);
				
				} elseif($eventData[$e['title']][$e['day']] == date("g:ia", $e['start']) . ' - ' . date("g:ia", $e['end'])) {
					
					$eventData[$e['title']][$e['day']] = $eventData[$e['title']][$e['day']];
				
				} else {
	
					$eventData[$e['title']][$e['day']] = $eventData[$e['title']][$e['day']] . ', ' . date("g:ia", $e['start']) . ' - ' . date("g:ia", $e['end']);
				
				}
			}
	    }
		$result = array();
	    foreach($eventData as $ed) {
	    	$result[] = $ed;
	    }
		
	    return $result;
	    
	}	
	
}
?>